package com.hqyj.yanguangzhi.controller;

import com.hqyj.common.pojo.Result;
import com.hqyj.common.pojo.SysUser;
import com.hqyj.common.pojo.SysUserRole;
import com.hqyj.common.pojo.TestPaper;
import com.hqyj.common.service.ISysUserRoleService;
import com.hqyj.common.util.CommonResult;
import com.hqyj.yanguangzhi.service.AMService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * YanGG    成绩管理
 *
 * @ClassName: ygzController
 * @author: yanguangzhi
 * @date: 2021/10/20 17:04
 */
@RestController
@RequestMapping("/am")
public class AMController {
    @Autowired
    private AMService amService;

    @Autowired
    private ISysUserRoleService userRoleService;

    /**
     * 读取成绩，先判断角色在进行查询
     *
     * @param session
     * @return
     */
    @GetMapping("/getAchievement")
    public CommonResult getAchievement(HttpSession session) {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(17);
        session.setAttribute("user", sysUser);

        SysUser user = (SysUser) session.getAttribute("user");
        SysUserRole userRole = userRoleService.queryUserRole(user.getUserId());
        if (userRole.getRoleId() == 1) {
            return amService.queryResultByUserId(user.getUserId());
        } else {
            return amService.queryResults(user);
        }
    }

    /**
     * 读取，试卷列表，根据角色判断
     *
     * @param session
     * @return
     */
    @GetMapping("/getTestPaperList")
    public CommonResult getTestPaperList(TestPaper testPaper, HttpSession session) {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(17);
        sysUser.setClazz("0701");
        session.setAttribute("user", sysUser);

        SysUser user = (SysUser) session.getAttribute("user");
        SysUserRole userRole = userRoleService.queryUserRole(user.getUserId());
        if (userRole.getRoleId() == 1) {
            return amService.queryTestPaperByUserId(testPaper, user.getUserId());
        }
        return amService.queryTestPaperByClazz(testPaper, user.getClazz());
    }

    /**
     * 根据试卷名排名
     *
     * @param result
     * @return
     */
    @GetMapping("/ranking")
    public CommonResult getRank(Result result) {
        return amService.queryResultByTestPaperAndNameWithDesc(result);
    }

    /**
     * 根据试卷名查询本场考试的平均成绩和通过率
     *
     * @param testPaperName
     * @return
     */
    @GetMapping("/avgAndPassRate")
    public CommonResult getAvgAndPassRate(String testPaperName) {
        return amService.queryAvgAndPassRateByTestPaperName(testPaperName);
    }
}
